package com.mall.server.service.pms;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.PmsProductApi;
import com.mall.dubbo.api.PmsProductAttributeApi;
import com.mall.dubbo.api.PmsProductCategoryAttributeRelationApi;
import com.mall.model.domain.PmsProduct;
import com.mall.model.domain.PmsProductAttribute;
import com.mall.model.vo.AttrInfoVo;
import com.mall.model.vo.CommonPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class PmsProductAttributeService {

    @DubboReference
    private PmsProductAttributeApi attributeApi;

    @DubboReference
    private PmsProductCategoryAttributeRelationApi relationApi;

    @DubboReference
    private PmsProductApi productApi;

    public List<AttrInfoVo > attrInfo(Integer productCategoryId) {
        //用productCategoryId查product
        List<PmsProduct> products=productApi.findByCategoryId(productCategoryId);
        //提取product里面的attributeCategoryId
        List<Long> attributeCategoryIds = CollUtil.getFieldValues(products, "productAttributeCategoryId", Long.class);
        Set<Long> setIds = new HashSet<>(attributeCategoryIds);
        attributeCategoryIds=new ArrayList<>(setIds);
        //使用attributeCategoryId查找attributeId
        List<PmsProductAttribute> attributes=attributeApi.findByAttributeCategoryIds(attributeCategoryIds);

        //创建前端所需AttrInfoVo集合
        List<AttrInfoVo> attrInfoVos = new ArrayList<>();
        //循环attributeCategoryIds
        for (Long attributeCategoryId : attributeCategoryIds) {
            //在循环attributes
            for (PmsProductAttribute attribute : attributes) {
                if (attribute.getProductAttributeCategoryId().equals(attributeCategoryId)){
                    AttrInfoVo attrInfoVo = new AttrInfoVo();
                    attrInfoVo.setAttributeCategoryId(attributeCategoryId);
                    attrInfoVo.setAttributeId(attribute.getId());
                }
            }
        }
        return attrInfoVos;
    }

    public void addAttribute(PmsProductAttribute attribute) {
        attributeApi.addAttribute(attribute);
    }

    public void deleteByIds(Long[] ids) {
        attributeApi.deleteByIds(ids);
    }

    public CommonPage<PmsProductAttribute> selectByCid(Integer cid, Integer type, Integer pageNum, Integer pageSize) {

        Long attributeCategoryId = Convert.toLong(cid);
        Page<PmsProductAttribute> attributeList=attributeApi.findByAttributeCategoryPage(attributeCategoryId,type,pageNum,pageSize);
        CommonPage<PmsProductAttribute> pageInfo = new CommonPage<>(attributeList);
        return pageInfo;
    }

    public void updateAttribute(Long id, PmsProductAttribute attribute) {
        PmsProductAttribute newAttribute = attributeApi.findById(id);
        attribute.setId(id);
        if (newAttribute==null){
            attributeApi.addAttribute(attribute);
        }
        attributeApi.updateAttribute(attribute);
    }

    public PmsProductAttribute findById(Long id) {
        PmsProductAttribute attribute = attributeApi.findById(id);
        return attribute;
    }
}
